package com.gopay.scheduler.job;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.cust.CustCaCertConstants;
import com.gopay.common.cps.dao.wallet.base.WaCertificateDao;
import com.gopay.common.domain.cust.CustCaCert;
import com.gopay.common.domain.dps.WaCertificate;
import com.gopay.common.exception.GopayException;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.cacert.CustCaCertService;
import com.gopay.scheduler.service.cacert.CustMobCaCertService;

/**
 * 客户数字证书更新提醒job
 * @CustCaCertUpdateRemind.java
 * @author xuangh
 * @2016年2月15日 下午4:15:44 www.gopay.com.cn Inc.All rights reserved.
 */
public class CustCaCertUpdateRemindJob extends GopayBaseJob {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource(name = "custCaCertServiceCron")
	private CustCaCertService certService;
	
	@Resource(name = "waCertificateDao")
	WaCertificateDao waCertificateDao;
	
	@Resource(name = "custMobCaCertService")
    private CustMobCaCertService custMobCaCertService;
	
	@Override
	protected void doJobInternal() {
		logger.error("custCaCertUpdateRemindJob------------start---------");
		logger.error("开始处理过期数字证书！");
		
		/**
		 * 获取所有过期的客服数字证书数据
		 */
		List<CustCaCert> list = certService.getLimitCustCaCert();
		if (list != null && list.size() > 0) {
			logger.error("查询获取到的过期数字证书的数量为： " + list.size());
			int successCount = 0;
			for (CustCaCert custCaCert : list) {
				String certId = custCaCert.getId().toString();
				try {
					int result = certService.updateCustCaCert(custCaCert, CustCaCertConstants.CUST_CA_CERT_STAT_LIMIT);
					if (result > 0) {
						successCount++;
						logger.info("第" + successCount + "条修改成功,certId: " + certId);
					}
				} catch (GopayException e) {
					logger.error("更新过期数字证书失败,certId:" + certId,e);
				} catch (Exception e) {
					logger.error("更新过期数字证书失败,certId:" + certId,e);
				}
			}
			logger.error("修改成功数： " + successCount+"处理过期数字证书结束。");
		}

		/**
		 * 因为前期移动数字证书已经有自己的一张表
		 * 所以copy出一套来更新
		 * 获取所有过期的移动数字证书数据
		 * add by zhoupeng 20171114
		 */
		List<WaCertificate> waCertificatelist = waCertificateDao.getLimitWaCertificate();
		if (waCertificatelist != null && waCertificatelist.size() > 0) {
			logger.info("查询获取到的过期移动数字证书的数量为： " + waCertificatelist.size());
			int successCount = 0;
			for (WaCertificate waCertificate : waCertificatelist) {
				try {
					int result = updateWaCertificate(waCertificate, CustCaCertConstants.CUST_CA_CERT_STAT_LIMIT);
					if (result > 0) {
						successCount++;
						logger.info("第" + successCount + "条修改成功");
					}
				} catch (Exception e) {
					logger.error("更新过期数字证书失败,Id:"+waCertificate.getId(),e);
				}
			}
			logger.info("修改成功数： " + successCount+"处理移动过期数字证书结束。");
		}
		
	}
	
	/**
	 * 更新移动数字证书状态
	 * @param waCertificate
	 * @param stat
	 * @return
	 * @throws GopayException
	 */
	private int updateWaCertificate(WaCertificate waCertificate, String stat) throws Exception {
		int resunt = 0;
		try {
			waCertificate.setStat(stat);
			waCertificate.setLastUptTs(new Date());
			waCertificateDao.update(waCertificate);
			resunt = 1;
		} catch (Exception e) {
			throw new Exception("修改数字证书过期状态失败！");
		}
		return resunt;

	}
}

